-- Lua Battle System - 消耗品

local Logger = require("utils.logger")
local Item = require("items.item")
local StatusEffects = require("characters.status_effects")

local Consumable = Item:new({
    -- 消耗品特有属性
    type = "consumable",
    stackable = true,
    max_stack = 99,
    
    -- 消耗品效果
    healing = {
        hp = 0,
        mp = 0
    },
    
    -- 状态效果
    status_effects = {
        target = {}, -- 对目标施加的状态效果
        user = {} -- 对使用者施加的状态效果
    }
})

-- 消耗品构造函数
function Consumable:new(o)
    o = o or {}
    setmetatable(o, self)
    self.__index = self
    o:init()
    return o
end

-- 初始化消耗品
function Consumable:init()
    Item.init(self)
    self.type = "consumable"
    self.stackable = true
    self.max_stack = 99
    self.healing = {
        hp = 0,
        mp = 0
    }
    self.status_effects = {
        target = {},
        user = {}
    }
    
    Logger.debug("消耗品初始化完成: " .. self.name)
end

-- 应用消耗品效果
function Consumable:apply_effects(user, target)
    local result = {
        success = true,
        effects_applied = {}
    }
    
    -- 恢复生命值
    if self.healing.hp > 0 then
        local actual_healing = target:heal(self.healing.hp)
        table.insert(result.effects_applied, {
            type = "healing",
            target = target.id,
            amount = actual_healing,
            stat = "hp"
        })
        Logger.info(target.name .. " 恢复了 " .. actual_healing .. " 点生命值")
    end
    
    -- 恢复魔法值
    if self.healing.mp > 0 then
        local actual_mp_recovery = target:recover_mp(self.healing.mp)
        table.insert(result.effects_applied, {
            type = "healing",
            target = target.id,
            amount = actual_mp_recovery,
            stat = "mp"
        })
        Logger.info(target.name .. " 恢复了 " .. actual_mp_recovery .. " 点魔法值")
    end
    
    -- 应用状态效果到目标
    for _, effect_info in ipairs(self.status_effects.target) do
        local effect = StatusEffects.create_effect(effect_info.id, effect_info.params)
        if effect then
            target:add_status_effect(effect)
            table.insert(result.effects_applied, {
                type = "status_effect",
                target = target.id,
                effect = effect.id
            })
            Logger.info(target.name .. " 获得了 " .. effect.name .. " 状态效果")
        end
    end
    
    -- 应用状态效果到使用者
    for _, effect_info in ipairs(self.status_effects.user) do
        local effect = StatusEffects.create_effect(effect_info.id, effect_info.params)
        if effect then
            user:add_status_effect(effect)
            table.insert(result.effects_applied, {
                type = "status_effect",
                target = user.id,
                effect = effect.id
            })
            Logger.info(user.name .. " 获得了 " .. effect.name .. " 状态效果")
        end
    end
    
    return result
end

-- 导出模块
return Consumable